"""
Case Type   : 硬件故障类--network
Case Name   : 备节点重建时，备节点错报率3%
Create At   : 2021/07/08
Owner       : 5328126
Description :
    1. 备节点注入网络错包故障
    2. 备节点重建
Expect      :
    1.成功
    2.成功
History     :
    Modified by @wan005 2022/2/27:网络错包会导致环境SSH异常，调整错包率规避该问题
    Modified by 1205145 2023/10/31:扩容前重建root互信
"""

import unittest
import time
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger


class NetWork(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case076:开始执行---------')
        self.sta1_root_node = Node(node='Standby1Root')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.com = Common()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.log.info("-------------获取网卡名---------------")
        network_card_cmd = 'ifconfig'
        self.log.info(network_card_cmd)
        network_card_result = self.sta1_root_node.sh(
            network_card_cmd).result()
        network_card_list = network_card_result.split('\n\n')
        self.log.info(network_card_list)
        self.networkcard = ''
        for network_card_item in network_card_list:
            if self.sta1_dbuser.db_host in network_card_item:
                self.networkcard = network_card_item.split(':')[0].strip()
                self.log.info(self.networkcard)

    def test_main(self):
        self.log.info("--------------1.注入网络错包故障-------------")
        text = '----step1：注入网络错包故障 expect:成功----'
        inject_command = f"rNet_corrupt " \
            f"(dev, rate)values({self.networkcard}, " \
            f"{self.constant.network_error_rate})"
        result = self.com.cfe_inject(self.sta1_root_node, inject_command)
        self.log.info(result)
        result = self.com.cfe_query(self.sta1_root_node,
                                    f"rNet_corrupt where "
                                    f"dev={self.networkcard}")
        self.log.info(result)
        self.assertIn(f"corrupt {self.constant.network_error_rate}",
                      result, '执行失败:' + text)

        self.log.info("-------------2. 备节点重建---------------")
        text = '----step2：备节点重建 expect:成功----'
        result = self.sta_sh.executeGsctl('build',
                                          self.constant.REBUILD_SUCCESS_MSG)
        self.assertTrue(result, '执行失败:' + text)
        db_status = self.primary_sh.getDbClusterStatus("status")
        self.assertTrue(db_status, '执行失败:' + text)

    def tearDown(self):
        self.log.info("--------------备节点清理故障-------------")
        result = self.com.cfe_clean(self.sta1_root_node,
                                    f"rNet_corrupt where "
                                    f"dev={self.networkcard} and rate="
                                    f"{self.constant.network_error_rate}")
        self.log.info(result)
        result = self.com.cfe_query(self.sta1_root_node,
                                    f"rNet_corrupt where "
                                    f"dev={self.networkcard}")
        time.sleep(5)
        db_status = self.primary_sh.getDbClusterStatus("status")
        self.log.info(db_status)
        self.log.info(result)
        self.log.info('---Opengauss_Reliability_Hardware_Case076:执行完成----')
